<?php

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    {   
        protected function _initView ()
        {
            // Initialize view
            $view = new Zend_View();
            $view->doctype('XHTML1_STRICT');
            $view->headTitle('Principio');
            $view->env = APPLICATION_ENV;
 
            // Add it to the ViewRenderer
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
            $viewRenderer->setView($view);
 
            // Return it, so that it can be stored by the bootstrap
            return $view;
        }



        protected function _initAutoload()
        {
            $loader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'Principio',
                'basePath' => APPLICATION_PATH
            ));

            $loader->addResourceType("library", "library/", "Library");
            return $loader;
        }


		
        protected function _initDhcpConfig()
        {
    		Zend_Registry::set('config_dhcp', $this->getOption('dhcp_config'));
            Zend_Registry::set("config_principio", $this->getOption("principio"));
        }

        public function run()
        {
            $auth = Zend_Auth::getInstance();
            $request_http = new Zend_Controller_Request_Http();
            $this->_initDhcpConfig();

            if (($request_http->isPost()) || (($request_http->isGet()) && (substr($request_http->getRequestUri(), 0, 5) != "/prv?")))
            {
                if ($auth->hasIdentity())
                {
                    // Authenticated
                    if ($request_http->getRequestUri() == "/")
                    {
                        // Non-Ajax request
                        Zend_Layout::startMvc();
                        $layout = Zend_Layout::getMvcInstance();
                        $layout->setLayout("main_layout");
                    }
                    else
                    {
                        Zend_Layout::getMvcInstance()->disableLayout();
                    };
                }
                else
                {
                    // Require authentication

                    if ($request_http->getRequestUri() != "/auth")
                    {
                        $front = Zend_Controller_Front::getInstance();
                        $response_http = new Zend_Controller_Response_Http();
                        $response_http->setRedirect("/auth");
                        $front->setResponse($response_http);
                    };
                };
            }
            else
            {
                Zend_Layout::getMvcInstance()->disableLayout();
            };
            
            return parent::run();
        }
}